package com.atguigu.spzx.user.service.impl;

import com.atguigu.spzx.common.interceptor.SpzxUserInfoInterceptor;
import com.atguigu.spzx.model.entity.user.UserAddress;
import com.atguigu.spzx.user.mapper.UserAddressMapper;
import com.atguigu.spzx.user.service.UserAddressService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import com.atguigu.spzx.user.service.UserAddressService;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Resource
    UserAddressMapper userAddressMapper;
    @Override
    public void updateById(UserAddress userAddress) {
        userAddress.setCreateTime(null);
        userAddress.setUpdateTime(null);
        userAddress.setFullAddress(userAddress.getAddress());
        //判断要将地址更新为默认地址
        //更新当前用户的所有的地址为非默认地址
        userAddressMapper.updateById(userAddress);
    }

    @Override
    public void save(UserAddress userAddress) {
        //设置userId
        userAddress.setUserId(SpzxUserInfoInterceptor.THREAD_LOCAL.get().getId());
        //拼接详细地址
        userAddress.setFullAddress(userAddress.getAddress());
        //判断是否新增默认地址
        //更新当前用户的所有的地址为非默认地址
        //如果已存在默认地址
        userAddressMapper.save(userAddress);
    }

    @Override
    public UserAddress getUserAddress(Long id) {
        UserAddress userAddress = userAddressMapper.getUserAddress(id);
        userAddress.setFullAddress(userAddress.getAddress());
        return userAddress;
    }

    @Override
    public List<UserAddress> findUserAddressList() {
        Long userId = SpzxUserInfoInterceptor.THREAD_LOCAL.get().getId();
        return userAddressMapper.findUserAddressListByUserId(userId);
    }

    @Override
    public void deleteById(Long id) {
        userAddressMapper.deleteById(id);
    }
}
